package com.jerry.constellation.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.jerry.constellation.R;
import com.jerry.constellation.Utils.AssetsUtils;
import com.jerry.constellation.Utils.ToastUtils;
import com.jerry.constellation.activity.PartnerDetialsActivity;
import com.jerry.constellation.bean.StarInfoBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class Partner_Fragment extends Fragment implements View.OnClickListener, AdapterView.OnItemSelectedListener {

    private ImageView maleIv;
    private ImageView femaleIv;
    private Spinner maleSp;
    private Spinner femaleSp;
    private Button prize;
    private Button play;
    List<String> nameList;//存放了星座名称的list集合
    private Map<String, Bitmap> contentlogimgMap;
    private List<StarInfoBean.StarinfoBean> infoList;
    private Bitmap bitmap;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.partner_fragment, container, false);
        //初始化组件
        initView(view);
        //设置监听
        setOnclick();
        //获取activity传递的数据
        Bundle bundle = getArguments();
        StarInfoBean starInfoBean = (StarInfoBean) bundle.getSerializable("info");
        infoList = starInfoBean.getStarinfo();
        nameList = new ArrayList<>();
        //获取适配器所需要的数据源
        for (int i = 0; i < infoList.size(); i++) {
            String name = infoList.get(i).getName();
            nameList.add(name);
        }

        //创建适配器
        ArrayAdapter<String> SpinnerArrayAdapter = new ArrayAdapter<>(getContext(), R.layout.item_partner_spinner_layout, R.id.item_partner_Tv, nameList);
        //设置适配器
        maleSp.setAdapter(SpinnerArrayAdapter);
        femaleSp.setAdapter(SpinnerArrayAdapter);

        //获取第一个图片显示在Iv上
        String logoname = infoList.get(0).getLogoname();
        contentlogimgMap = AssetsUtils.getContentlogimgMap();
        Bitmap bitmap = contentlogimgMap.get(logoname);
        maleIv.setImageBitmap(bitmap);
        femaleIv.setImageBitmap(bitmap);

        return view;
    }

    private void setOnclick() {
        prize.setOnClickListener(this);
        play.setOnClickListener(this);
        maleSp.setOnItemSelectedListener(this);
        femaleSp.setOnItemSelectedListener(this);

    }

    private void initView(View view) {
        maleIv = view.findViewById(R.id.partnerFrg_Iv_male);
        femaleIv = view.findViewById(R.id.partnerFrg_Iv_female);
        maleSp = view.findViewById(R.id.partnerFrg_Sp_male);
        femaleSp = view.findViewById(R.id.partnerFrg_Sp_female);
        prize = view.findViewById(R.id.partnerFrg_Bt_prize);
        play = view.findViewById(R.id.partnerFrg_Bt_play);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.partnerFrg_Bt_prize:
                ToastUtils.showImageToas(getActivity(), "还不行哟~");
                break;
            case R.id.partnerFrg_Bt_play:
                //获取男女Spanner选择的位置
                int malePos = maleSp.getSelectedItemPosition();
                int femalePos = femaleSp.getSelectedItemPosition();
                //通过位置 跳转传值到星座详情页面
                Intent intent = new Intent(getContext(), PartnerDetialsActivity.class);
                intent.putExtra("male_name", infoList.get(malePos).getName());
                intent.putExtra("male_logoname", infoList.get(malePos).getLogoname());
                intent.putExtra("female_name", infoList.get(femalePos).getName());
                intent.putExtra("female_logoname", infoList.get(femalePos).getLogoname());
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        switch (adapterView.getId()) {
            case R.id.partnerFrg_Sp_male:
                String logoname = infoList.get(i).getLogoname();
                Bitmap bitmap = contentlogimgMap.get(logoname);
                maleIv.setImageBitmap(bitmap);
                break;
            case R.id.partnerFrg_Sp_female:
                String logoname_female = infoList.get(i).getLogoname();
                Bitmap bitmap_female = contentlogimgMap.get(logoname_female);
                femaleIv.setImageBitmap(bitmap_female);
                break;

        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
}
